
package Controleur;

//aka orignator:memento

//TODO Fix OVERWRITE Replay

//TODO Add Interface for controlleur
import Modele.*;
import minipad.GetViewInterface;
import minipad.UpdateViewReceiver;

public class Controleur {

    private ReplayMgr replaylist;

//    private UpdateViewReceiver uvr;
    private UpdateHistoryReceiver uhr;
    private GetViewInterface gvi;

    //Commande
    private InsererCmd insert_cmd;
    private UndoCmd undo_cmd;
    private RedoCmd redo_cmd;
    private DeleteCmd delete_cmd;
    private SelectCmd select_cmd;
    private CopyCmd copy_cmd;
    private PasteCmd paste_cmd;
    private CutCmd cut_cmd;

    public Controleur(UpdateViewReceiver u)  {
        replaylist = new ReplayMgr(u);
        //On instancie une nouvelle liste d'historique
        uhr = new GestionHistorique("", 0, 0, false, 0, "", null);
        uhr.setCtrl(this);

        //Instanciation des commandes
        insert_cmd = new InsererCmd(u,uhr);
        undo_cmd = new UndoCmd(u, uhr);
        redo_cmd = new RedoCmd(u, uhr);
        delete_cmd = new DeleteCmd(u, uhr);
        select_cmd = new SelectCmd(u, uhr);
        copy_cmd = new CopyCmd(u, uhr);
        paste_cmd = new PasteCmd(u, uhr);
        cut_cmd = new CutCmd(u, uhr);
    }

    public void setUhr(UpdateHistoryReceiver uhr) {
        this.uhr = uhr; }

    /*
    public void setUvr(UpdateViewReceiver uvr) {
        this.uvr = uvr; } */

    public void setgvi(GetViewInterface g){
        gvi = g; }

    public boolean isListEmpty(){
        return replaylist.isListEmpty();
    }

    /**************************************
     * Exécution des commandes
     **************************************/
    public void inserer() {
        insert_cmd.execute();
        if (getMacroEnabled()) replaylist.add(insert_cmd,getVLastChar());
    }

    public void undo() {
        undo_cmd.execute();
    }

    public void redo() {
        redo_cmd.execute();
    }

    public void delete() {
        delete_cmd.execute();
        //FIXME delete replay and others
        if (getMacroEnabled()) replaylist.add(delete_cmd,getVLastChar());
    }

    public void rejouer(){
        if (!getMacroEnabled())
            replaylist.play();
    }

    public void select(){
        select_cmd.execute();
    }

    public void copy(){
        copy_cmd.execute();
    }

    public void paste(){
        paste_cmd.execute();
    }

    public void cut(){
        cut_cmd.execute();
    }

    /**************************************
    *    Get State buffer
    **************************************/
    public String getMBuffer (){
        return uhr.getMBuffer();
    }

    public int getMCurrentPos(){
        return uhr.getMCurrentPos();
    }

    public Character getMLastCharacter(){
        return uhr.getMLastChar();
    }

    public boolean isUndoable(){
        return uhr.isUndoable();
    }

    public boolean isRedoable() {
        return uhr.isRedoable();
    }

    public int getMSelect_fin(){
        return uhr.getMSelect_fin();
    }

    public int getMSelect_debut(){
        return uhr.getMSelect_debut();
    }

    public String getMPressePapier(){
        return uhr.getPressePapier();
    }

    /**************************************
    * Get View GVI - Get view from internal data
    ***************************************/
    public String getVBuffer(){
        return gvi.getText();
    }

    public Character getVLastChar(){
        return gvi.getLastChar();
    }

    public int getVCurseurPosition(){
        return gvi.getPosition_cursor();
    }

    public boolean getMacroEnabled (){
        return gvi.getMacroEnabled();
    }

    public int getVSelect_fin(){
        return gvi.getSelect_fin();
    }

    public boolean isVTextSelected() {
        return gvi.isTextSelected();
    }

    public String getVPressePapier(){
        return gvi.getPressePapier();
    }

}
